#!/usr/bin/env python
# -*- coding: utf-8 -*-

from jujuz.protocol import Protocol, Factory

class   EchoProtocol(Protocol) :
	
    def OnConnectionMade(self) :
        self.factory.count_protocols = self.factory.count_protocols + 1
        if self.factory.count_protocols > 100 :
            self.transport.write( 'Too many connections, try again later.' )
            self.transport.loseConnection()
        # OnConnectionMade()

    def OnConnectionLost(self) :
        self.factory.count_protocols -= 1
        # OnConnectionLost()

    def OnDataReceived(self, data) :
        self.transport.write(data)
        # OnDataReceived()

    # class EchoProtocol()

factory = Factory()
factory.protocol = EchoProtocol
factory.count_protocols = 0

from jujuz.reactor import Reactor
reactor = Reactor()
reactor.listenTCP(4344, factory)
reactor.run()
